RegEx SETS



Python - RegEx Sets


A set is a set of characters inside a pair of square brackets [] with a special meaning.


SetDescription
[arn]Returns a match where one of the specified characters (a, r, or n) are present

import re
str="she sells sea shells on the sea shore"

#Check if the string has any a, r, or n characters:

x=re.findall("[arn]",str)
print(x)
if(x):
  print("yes,there is atleast one match")
else:
  print("no match")
  
==========o/p==========

['a', 'n', 'a', 'r']
yes,there is atleast one match

SetDescription
[a-n]Returns a match for any lower case character, alphabetically between a and n

import re
str="She sells sea shells on the sea shore"

#Check if the string has any characters between a and n:

x=re.findall("[a-n]",str)
print(x)
if(x):
  print("yes,there is atleast one match")
else:
  print("no match")
  

==========O/P==============

['h', 'e', 'e', 'l', 'l', 'e', 'a', 'h', 'e', 'l', 'l', 'n', 'h', 'e', 'e', 'a', 'h', 'e']
yes,there is atleast one match


SetDescription
[^arn]Returns a match for any character EXCEPT a, r, and n

import re
str="She sells sea shells on the sea shore"

#Check if the string has other characters than a, r, or n:

x=re.findall("[^arn]",str)
print(x)
if(x):
  print("yes,there is atleast one match")
else:
  print("no match")


============O/P=============

['S', 'h', 'e', ' ', 's', 'e', 'l', 'l', 's', ' ', 's', 'e', ' ', 's', 'h', 'e', 'l', 'l', 's', ' ', 'o', ' ', 't', 'h', 'e', ' ', 's', 'e', ' ', 's', 'h', 'o', 'e']
yes,there is atleast one match

SetDescription
[0123]Returns a match where any of the specified digits (0, 1, 2, or 3) are present

import re
str="She sells sea shells on the sea shore"

#Returns a match where any of the specified digits (0, 1, 2, or 3) are present
x=re.findall("[0123]",str)
print(x)
if(x):
  print("yes,there is atleast one match")
else:
  print("no match")


===========o/p============

[]
no match

SetDescription
[0-9]Returns a match for any digit between 0 and 9

import re
str="The time is 6:19 PM"

#Check if the string has any digits:

x=re.findall("[0-9]",str)
print(x)
if(x):
  print("yes,there is atleast one match")
else:
  print("no match")

===========O/P===========

['6', '1', '9']
yes,there is atleast one match

SetDescription
[0-5][0-9]Returns a match for any two-digit numbers from 00 and 59

import re
str="The time is 18:19 PM"

#Check if the string has any two-digit numbers, from 00 to 59:

x=re.findall("[0-5][0-9]",str)
print(x)
if(x):
  print("yes,there is atleast one match")
else:
  print("no match")


==========O/P===========

['18', '19']
yes,there is atleast one match

SetDescription
[a-zA-Z]Returns a match for any character alphabetically between a and z, lower case OR upper case

import re
str="The time is 18:19 PM"

#Check if the string has any characters from a to z lower case, and A to Z upper case:

x=re.findall("[a-zA-Z]",str)
print(x)
if(x):
  print("yes,there is atleast one match")
else:
  print("no match")


===========O/P=============

['T', 'h', 'e', 't', 'i', 'm', 'e', 'i', 's', 'P', 'M']
yes,there is atleast one match

SetDescription
[+]In sets, +, *, .,(), $,{} has no special meaning, so [+] means: return a match for any + character in the string

import re
str="The time is 18:19 PM"

#Check if the string has any + characters:

x=re.findall("[+]",str)
print(x)
if(x):
  print("yes,there is atleast one match")
else:
  print("no match")

===========O/P============

[]
no match



RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials